package com.bjzc.AndroidSDK;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LvAdapter extends BaseAdapter {
    private List<HashMap<String, String>> testList = new ArrayList<HashMap<String, String>>();
    private static Activity context;
    BitmapDrawable bitmap1;
    static String str = "";

    public LvAdapter(Activity context, List<HashMap<String, String>> testList) {
        this.context = context;
        this.testList = testList;
    }

    @Override
    public int getCount() {
        int size = 0;
        if (testList != null) {
            size = testList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        if (testList.size() > 0)
            return testList.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private Integer index = -1;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        class MyTextWatcher implements TextWatcher {
            private ViewHolder mHolder;
            private EditText mEditText;

            public MyTextWatcher(ViewHolder holder, EditText edt) {
                mHolder = holder;
                mEditText = edt;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && !"".equals(s.toString())) {
                    if (mEditText == mHolder.edit1) {
                        int position = (Integer) mHolder.edit1.getTag();
                        testList.get(position).put("edit1", s.toString());
                    }
                    if (mEditText == mHolder.edit2) {
                        int position = (Integer) mHolder.edit2.getTag();
                        testList.get(position).put("edit2", s.toString());
                    }
                }
            }
        }

        final ViewHolder vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.lv_item, null);
            vh = new ViewHolder();
            vh.layout = (RelativeLayout) convertView.findViewById(R.id.lvlayout);
            vh.name = (TextView) convertView.findViewById(R.id.name);
            vh.edit1 = (EditText) convertView.findViewById(R.id.edit1);
            vh.edit1.setTag(position);
            vh.edit1.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    index = (Integer) v.getTag();
                }
            });
            vh.edit1.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        index = (Integer) v.getTag();
                    }
                    return false;
                }
            });
            vh.edit1.addTextChangedListener(new MyTextWatcher(vh, vh.edit1));
            ////
            vh.edit2 = (EditText) convertView.findViewById(R.id.edit2);
            vh.edit2.setTag(position);
            vh.edit2.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    index = (Integer) v.getTag();
                }
            });
            vh.edit2.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        index = (Integer) v.getTag();
                    }
                    return false;
                }
            });
            vh.edit2.addTextChangedListener(new MyTextWatcher(vh, vh.edit2));
            /////
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
            vh.edit1.setTag(position);
            vh.edit2.setTag(position);
        }
        vh.name.setText(testList.get(position).get("name"));


        if (position % 2 == 0) {
            vh.layout.setBackgroundColor(Color.CYAN);
        } else {
            vh.layout.setBackgroundColor(Color.TRANSPARENT);
        }

        vh.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//				if (position % 2 == 0) {
//					vh.layout.setBackgroundResource(Color.BLUE);
//				} else {
//					vh.layout.setBackgroundResource(Color.TRANSPARENT);
//				}
                //open(position);
            }
        });


        Object value = testList.get(position).get("edit1");
        if (value != null && !"".equals(value)) {
            vh.edit1.setText(value.toString());
        } else {
            vh.edit1.setText("");
        }
        Object value2 = testList.get(position).get("edit2");
        if (value2 != null && !"".equals(value2)) {
            vh.edit2.setText(value2.toString());
        } else {
            vh.edit2.setText("");
        }
/*		vh.xindei.clearFocus();
        if (index != -1 && index == position) {
			vh.xindei.requestFocus();
		}*/

        return convertView;
    }


    public class ViewHolder {
        TextView name;
        EditText edit1;
        EditText edit2;
        RelativeLayout layout;
    }

    public void open(int position) {
/*		Intent intent = new Intent(context,OtherActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("pos", position+"");
		context.startActivity(intent);*/

    }


}
